import './App.css';
import {createContext,useContext, useRef, useState} from "react";

let MsgContext = createContext();

function A(props) {
    let msg = useContext(MsgContext);
    return (
        <div>
            <h1>组件A, 接受：{msg}</h1>
            <B></B>
        </div>
    )
}
function B(props) {
    let msg = useContext(MsgContext);
    console.log(msg)
    return (
        <div>
            <h1>组件B, 接受：{msg}</h1>
        </div>
    )
}

function App() {

    let name = '张三';
    /**
     * 注意:
     * createContext 只能父传子或传孙子
     */
    return (
        <div>
            <MsgContext.Provider value={name}>
            <A></A>
            </MsgContext.Provider>
        </div>
    )
}

export default App;